package com.zonski.jbots.desktop.editer;

import com.zonski.jbots.desktop.client.PainterCanvas;
import com.zonski.jbots.desktop.client.RoomRenderer;
import com.zonski.jbots.desktop.game.JBotsRoomRendererFactory;
import com.zonski.jbots.desktop.application.ApplicationDevice;
import com.zonski.jbots.engine.FileInputStreamFactory;
import com.zonski.jbots.game.JBotsEntityFactory;
import com.zonski.jbots.game.JBotsConstants;
import com.zonski.jbots.engine.Engine;
import com.zonski.jbots.engine.Device;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 27, 2003
 * Time: 8:48:16 AM
 * To change this template use Options | File Templates.
 * <p>
 * Editer for the level files for jbots
 */
public class JBotsDesktopEditer
{
    public static final void main(String[] args)
    {
        final JBotsDesktopEditer editer = new JBotsDesktopEditer();

        editer.mainFrame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent event)
            {
                if(editer.context.getRoom() == null || editer.operations.querySave())
                {
                    System.exit(0);
                }
            }
        });
        editer.mainFrame.setVisible(true);
    }

    private JFrame mainFrame;
    private EditerCanvas canvas;
    private JBotsEditerContext context;
    private EditerOperations operations;
    private EditerActions actions;

    public JBotsDesktopEditer()
    {
        initLayout();
    }

    private final void initLayout()
    {

        JComboBox comboBox = new JComboBox(new Object[]{"0", "1", "2", "3", "4", "5"});
        this.canvas = new EditerCanvas();
        RoomRenderer roomRenderer = (new JBotsRoomRendererFactory(this.canvas)).createRoomRenderer();
        this.canvas.setRoomRenderer(roomRenderer);
        Device device = new ApplicationDevice(new FileInputStreamFactory("sounds/", "wav"));
        Engine engine = new Engine(new JBotsEntityFactory(device), device);

        // load the players
        for(int i=0; i<4; i++)
        {
            String name = Engine.getPlayerMindName(i);
            engine.setMind(name, new EditerMind(name));
        }

        this.context = new JBotsEditerContext(this.canvas, engine, comboBox);
        this.canvas.setContext(this.context);
        JLabel status = new JLabel("JBots Editor");
        status.setPreferredSize(new Dimension(100, 16));
        this.operations = new EditerOperations(this.context, engine, status);
        this.actions = new EditerActions(this.operations,
                roomRenderer.getEntityRendererFactory(),
                roomRenderer.getTileRenderer());

        this.context.setOperations(this.operations);

        JFrame frame = new JFrame("JBots Editer");
        JMenuBar menuBar = new JMenuBar();
        JToolBar toolBar = new JToolBar();
        JPanel panel = new JPanel(new BorderLayout());
        JPanel tilePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.setPreferredSize(new Dimension(180, 200));

        panel.add(tilePanel, BorderLayout.CENTER);
        JCheckBox checkBox = new JCheckBox("Centre Entities");
        checkBox.setSelected(this.context.isCentredOnTile());
        checkBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                JCheckBox checkBox = (JCheckBox)event.getSource();
                JBotsDesktopEditer.this.context.setCentredOnTile(checkBox.isSelected());
            }
        });
        panel.add(checkBox, BorderLayout.SOUTH);
        comboBox.setEditable(true);
        comboBox.setSelectedItem(Integer.toString(this.context.getCurrentLevel()));
        comboBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                JComboBox comboBox = (JComboBox)event.getSource();
                String layer = (String)comboBox.getSelectedItem();
                try
                {
                    int layerId = Integer.parseInt(layer);
                    JBotsDesktopEditer.this.context.setCurrentLevel(layerId);
                }catch(Exception ex){
                    comboBox.setSelectedIndex(0);
                }
            }
        });
        panel.add(comboBox, BorderLayout.NORTH);



        initMenuBar(menuBar);
        initToolBar(toolBar);
        initTilePanel(tilePanel);

        this.canvas.setBorder(BorderFactory.createBevelBorder(2));

        frame.setJMenuBar(menuBar);
        frame.setSize(640, 480);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(toolBar, BorderLayout.NORTH);
        frame.getContentPane().add(this.canvas, BorderLayout.CENTER);
        frame.getContentPane().add(status, BorderLayout.SOUTH);
        frame.getContentPane().add(panel, BorderLayout.EAST);

        this.mainFrame = frame;
    }

    private final void initMenuBar(JMenuBar menuBar)
    {
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);

        JMenuItem fileNewMenu = new JMenuItem("New");
        JMenuItem fileOpenMenu = new JMenuItem("Open...");
        JMenuItem fileSaveMenu = new JMenuItem("Save");
        JMenuItem fileSaveAsMenu = new JMenuItem("Save As...");
        JMenuItem fileExitMenu = new JMenuItem("Exit");

        fileNewMenu.setAction(actions.getNewAction());
        fileOpenMenu.setAction(actions.getOpenAction());
        fileSaveMenu.setAction(actions.getSaveAction());
        fileSaveAsMenu.setAction(actions.getSaveAsAction());

        fileMenu.add(fileNewMenu);
        fileMenu.add(fileOpenMenu);
        fileMenu.add(fileSaveMenu);
        fileMenu.add(fileSaveAsMenu);
        fileMenu.addSeparator();
        fileMenu.add(fileExitMenu);

        JMenu roomMenu = new JMenu("Room");
        menuBar.add(roomMenu);

        JMenuItem roomNameMenu = new JMenuItem("Room Name...");
        JMenuItem roomMusicMenu = new JMenuItem("Room Music...");

        roomNameMenu.setAction(actions.getRenameAction());
        roomMusicMenu.setAction(actions.getChangeMusicAction());

        roomMenu.add(roomNameMenu);
        roomMenu.add(roomMusicMenu);

        JMenu toolMenu = new JMenu("Tools");
        menuBar.add(toolMenu);

        JMenuItem toolRockMenu = new JCheckBoxMenuItem();
        JMenuItem toolTempRockMenu = new JCheckBoxMenuItem();
        JMenuItem toolTreeMenu = new JCheckBoxMenuItem();
        JMenuItem toolChargeMenu = new JCheckBoxMenuItem();
        JMenuItem toolChargeStationMenu = new JCheckBoxMenuItem();
        JMenuItem toolCapacityMenu = new JCheckBoxMenuItem();

        JMenu toolBotsMenu = new JMenu("Bots");

        JMenuItem toolBotsBombMenu = new JCheckBoxMenuItem();
        JMenuItem toolBotsWormMenu = new JCheckBoxMenuItem();
        JMenuItem toolBotsBabyMenu = new JCheckBoxMenuItem();
        JMenuItem toolBotsTurretMenu = new JCheckBoxMenuItem();
        JMenuItem toolBotsTankMenu = new JCheckBoxMenuItem();
        JMenuItem toolBotsBadMenu = new JCheckBoxMenuItem();

        toolBotsBombMenu.setAction(actions.getCreateAction(JBotsConstants.BOMB_BOT, toolBotsBombMenu));
        toolBotsWormMenu.setAction(actions.getCreateAction(JBotsConstants.CONVERTER_BOT, toolBotsWormMenu));
        toolBotsBabyMenu.setAction(actions.getCreateAction(JBotsConstants.BABY_BOT, toolBotsBabyMenu));
        toolBotsTurretMenu.setAction(actions.getCreateAction(JBotsConstants.TURRET_BOT, toolBotsTurretMenu));
        toolBotsTankMenu.setAction(actions.getCreateAction(JBotsConstants.TANK_BOT, toolBotsTankMenu));
        toolBotsBadMenu.setAction(actions.getCreateAction(JBotsConstants.BOT, toolBotsBadMenu));

        toolBotsMenu.add(toolBotsBombMenu);
        toolBotsMenu.add(toolBotsWormMenu);
        toolBotsMenu.add(toolBotsBabyMenu);
        toolBotsMenu.add(toolBotsTurretMenu);
        toolBotsMenu.add(toolBotsTankMenu);
        toolBotsMenu.addSeparator();
        toolBotsMenu.add(toolBotsBadMenu);

        toolRockMenu.setAction(actions.getCreateAction(JBotsConstants.ROCK, toolRockMenu));
        toolTempRockMenu.setAction(actions.getCreateAction(JBotsConstants.TEMP_ROCK, toolTempRockMenu));
        toolTreeMenu.setAction(actions.getCreateAction(JBotsConstants.TREE, toolTreeMenu));
        toolChargeMenu.setAction(actions.getCreateAction(JBotsConstants.CHARGE, toolChargeMenu));
        toolChargeStationMenu.setAction(actions.getCreateAction(JBotsConstants.RECHARGE, toolChargeStationMenu));
        toolCapacityMenu.setAction(actions.getCreateAction(JBotsConstants.CAPACITY, toolCapacityMenu));

        toolMenu.add(toolRockMenu);
        toolMenu.add(toolTempRockMenu);
        toolMenu.add(toolTreeMenu);
        toolMenu.addSeparator();
        toolMenu.add(toolChargeMenu);
        toolMenu.add(toolChargeStationMenu);
        toolMenu.add(toolCapacityMenu);
        toolMenu.addSeparator();
        toolMenu.add(toolBotsMenu);

        toolMenu.addSeparator();


        JMenuItem toolMoveMenu = new JCheckBoxMenuItem();
        toolMoveMenu.setAction(actions.getMoveAction(toolMoveMenu));
        toolMenu.add(toolMoveMenu);

        toolMenu.addSeparator();

        JMenuItem toolDeleteMenu = new JCheckBoxMenuItem();
        toolDeleteMenu.setAction(actions.getDeleteAction(toolDeleteMenu));
        toolMenu.add(toolDeleteMenu);
    }

    private void initToolBar(JToolBar toolBar)
    {
        JToggleButton rockButton = new JToggleButton();
        rockButton.setAction(this.actions.getCreateAction(JBotsConstants.ROCK, rockButton));
        JToggleButton tempRockButton = new JToggleButton();
        tempRockButton.setAction(this.actions.getCreateAction(JBotsConstants.TEMP_ROCK, tempRockButton));
        JToggleButton treeButton = new JToggleButton();
        treeButton.setAction(this.actions.getCreateAction(JBotsConstants.TREE, treeButton));
        JToggleButton chargeButton = new JToggleButton();
        chargeButton.setAction(this.actions.getCreateAction(JBotsConstants.CHARGE, chargeButton));
        JToggleButton rechargeButton = new JToggleButton();
        rechargeButton.setAction(this.actions.getCreateAction(JBotsConstants.RECHARGE, rechargeButton));

        toolBar.add(rockButton);
        toolBar.add(tempRockButton);
        toolBar.add(treeButton);
        toolBar.add(chargeButton);
        toolBar.add(rechargeButton);

        toolBar.addSeparator();

        JToggleButton moveButton = new JToggleButton();
        moveButton.setAction(this.actions.getMoveAction(moveButton));
        toolBar.add(moveButton);

        JToggleButton deleteButton = new JToggleButton();
        deleteButton.setAction(this.actions.getDeleteAction(deleteButton));
        toolBar.add(deleteButton);

    }

    private void initTilePanel(JPanel panel)
    {
        int i = 0;
        Action action;
        do
        {
            JToggleButton button = new JToggleButton();
            action = this.actions.getTileAction(i, button);
            if(action != null)
            {
                button.setAction(action);
                panel.add(button);
            }
            i++;
        }while(action != null);
    }
}
